# Web3管理平台 - 简化部署命令
# 使用方法: make deploy / make status / make logs

.PHONY: help install deploy status logs restart stop update backup clean

# 默认目标
.DEFAULT_GOAL := help

# 颜色定义
GREEN := \033[0;32m
BLUE := \033[0;34m
YELLOW := \033[1;33m
NC := \033[0m

help: ## 显示帮助信息
	@echo ""
	@echo "$(GREEN)Web3管理平台 - 部署命令$(NC)"
	@echo "════════════════════════════════════════"
	@echo ""
	@echo "$(BLUE)🚀 部署相关:$(NC)"
	@echo "  make install   - 一键安装 (首次部署)"
	@echo "  make deploy    - 智能部署 (推荐)"
	@echo "  make upgrade   - 升级系统"
	@echo ""
	@echo "$(BLUE)📊 管理相关:$(NC)"
	@echo "  make status    - 查看状态"
	@echo "  make logs      - 查看日志"
	@echo "  make restart   - 重启服务"
	@echo "  make stop      - 停止服务"
	@echo ""
	@echo "$(BLUE)🔧 工具相关:$(NC)"
	@echo "  make backup    - 备份数据"
	@echo "  make clean     - 清理资源"
	@echo "  make dashboard - 实时面板"
	@echo ""

install: ## 一键安装 (curl | bash)
	@echo "$(GREEN)🚀 执行一键安装...$(NC)"
	@chmod +x install.sh
	@./install.sh

deploy: ## 智能部署
	@echo "$(GREEN)🚀 执行智能部署...$(NC)"
	@chmod +x smart-deploy.sh
	@./smart-deploy.sh

status: ## 查看服务状态
	@echo "$(BLUE)📊 检查服务状态...$(NC)"
	@docker-compose ps 2>/dev/null || echo "服务未启动"
	@echo ""
	@echo "$(BLUE)🔍 健康检查:$(NC)"
	@curl -s http://localhost:8080/actuator/health >/dev/null 2>&1 && echo "✅ 后端: 正常" || echo "❌ 后端: 异常"
	@curl -s http://localhost/ >/dev/null 2>&1 && echo "✅ 前端: 正常" || echo "❌ 前端: 异常"

logs: ## 查看实时日志
	@echo "$(BLUE)📋 实时日志 (Ctrl+C退出):$(NC)"
	@docker-compose logs -f

restart: ## 重启所有服务
	@echo "$(YELLOW)🔄 重启服务...$(NC)"
	@docker-compose restart
	@echo "✅ 重启完成"

stop: ## 停止所有服务
	@echo "$(YELLOW)⏹️  停止服务...$(NC)"
	@docker-compose down
	@echo "✅ 服务已停止"

upgrade: ## 升级系统
	@echo "$(GREEN)📈 升级系统...$(NC)"
	@./smart-deploy.sh --upgrade

backup: ## 备份数据
	@echo "$(BLUE)💾 创建备份...$(NC)"
	@mkdir -p backups/$(shell date +%Y%m%d_%H%M%S)
	@docker exec web3-mysql mysqldump -u root -p$$MYSQL_ROOT_PASSWORD web3_management > backups/$(shell date +%Y%m%d_%H%M%S)/database.sql
	@cp .env backups/$(shell date +%Y%m%d_%H%M%S)/
	@echo "✅ 备份完成: backups/$(shell date +%Y%m%d_%H%M%S)/"

clean: ## 清理Docker资源
	@echo "$(YELLOW)🧹 清理Docker资源...$(NC)"
	@docker system prune -f
	@echo "✅ 清理完成"

dashboard: ## 打开实时监控面板
	@echo "$(GREEN)📊 启动实时面板...$(NC)"
	@./smart-deploy.sh --dashboard

# 开发相关命令
dev-setup: ## 开发环境设置
	@echo "$(BLUE)🛠️  设置开发环境...$(NC)"
	@cp .env.example .env
	@docker-compose -f docker-compose.dev.yml up -d
	@echo "✅ 开发环境就绪"

dev-logs: ## 开发环境日志
	@docker-compose -f docker-compose.dev.yml logs -f

# 快速命令别名
up: deploy  ## 别名: 部署
down: stop  ## 别名: 停止
ps: status  ## 别名: 状态